from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime, Float, Table
from sqlalchemy.orm import relationship
from app.config import Base
import datetime
from sqlalchemy.orm import column_property
from sqlalchemy import select, func
from sqlalchemy.sql import func



class Detector(Base):
    __tablename__ = 'detectors'

    id = Column(String, primary_key=True)
    coord_x = Column(Integer)
    coord_y = Column(Integer)

    anomalies = relationship("Anomaly", back_populates="detector")

class Anomaly(Base):
    __tablename__ = 'anomalys'
    id_nn = Column(Integer,primary_key=True, index=True)
    id = Column(String)
    g = Column(Float)
    detector_id = Column(String, ForeignKey("detectors.id"))
    x_detector_coord = column_property(select([Detector.coord_x]).where(Detector.id==detector_id))
    y_detector_coord = column_property(select([Detector.coord_y]).where(Detector.id==detector_id))

    detector = relationship("Detector", back_populates="anomalies")

class Category(Base):
    __tablename__ = "categories"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String)

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    email = Column(String, unique=True, index=True)
    hashed_password = Column(String)

    transactions = relationship("Transaction", back_populates="owner")


class Transaction(Base):
    __tablename__ = "transactions"

    id = Column(Integer, primary_key=True, index=True)
    timestamp = Column(DateTime, default=func.now())
    cost = Column(Float)
    name = Column(String)
    category_id = Column(Integer, ForeignKey("categories.id"))
    owner_id = Column(Integer, ForeignKey("users.id"))

    owner = relationship("User", back_populates="transactions")



